<?php

namespace Org\Util;

class PageForIndex {
	private $total; // 数据表中总记录数
	private $listRows; // 每页显示行数
	private $limit;
	private $uri;
	private $pageNum; // 页数
	private $config = array (
			'header' => "个记录",
			"prev" => "上一页",
			"next" => "下一页",
			"first" => "首 页",
			"last" => "尾 页" 
	);
	private $listNum = 8;
	private $htmlShow; // 分页需要显示的html文本数组
	
	/*
	 * $total $listRows
	 */
	public function __construct($listRows = 10, $pa = "") {
		$this->listRows = $listRows;
		$this->uri = $this->getUri ( $pa );
		$this->page = ! empty ( $_GET ["page"] ) ? $_GET ["page"] : 1;
	}
	public function init($total) {
		$this->total = $total;
		$this->pageNum = ceil ( $this->total / $this->listRows );
		$this->limit = $this->setLimit ();
	}
	private function setLimit() {
		return "Limit " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
	}
	private function getUri($pa) {
		$url = $_SERVER ["REQUEST_URI"] . (strpos ( $_SERVER ["REQUEST_URI"], '?' ) ? '' : "?") . $pa;
		$parse = parse_url ( $url );
		
		if (isset ( $parse ["query"] )) {
			parse_str ( $parse ['query'], $params );
			unset ( $params ["page"] );
			$url = $parse ['path'] . '?' . http_build_query ( $params );
		}
		
		return $url;
	}
	function __get($args) {
		if ($args == "limit")
			return $this->limit;
		else
			return null;
	}
	private function start() {
		if ($this->total == 0)
			return 0;
		else
			return ($this->page - 1) * $this->listRows + 1;
	}
	private function end() {
		return min ( $this->page * $this->listRows, $this->total );
	}
	private function first() {
		$html = "";
		if ($this->page == 1)
			$html .= '';
		else if (isset ( $this->htmlShow [3] )) {
			$this->htmlShow [3] = str_replace ( "%uri", $this->uri, $this->htmlShow [3] );
			$html .= $this->htmlShow [3];
		} else {
			$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;";
		}
		
		return $html;
	}
	private function prev() {
		$html = "";
		if ($this->page == 1)
			$html .= '';
		else if (isset ( $this->htmlShow [4] )) {
			$this->htmlShow [4] = str_replace ( "%uri", $this->uri, $this->htmlShow [4] );
			$html .= str_replace ( "%page", $this->page - 1, $this->htmlShow [4] );
		} else {
			$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page - 1) . "'>{$this->config["prev"]}</a>&nbsp;&nbsp;";
		}
		
		return $html;
	}
	private function pageList() {
		$linkPage = "";
		
		$inum = floor ( $this->listNum / 2 );
		
		for($i = $inum; $i >= 1; $i --) {
			$page = $this->page - $i;
			
			if ($page < 1)
				continue;
			if (isset ( $this->htmlShow [5] )) {
				$data = str_replace ( "%page", $page, $this->htmlShow [5] );
				$linkPage .= str_replace ( "%uri", $this->uri, $data );
			} else {
				$linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
			}
		}
		
		// 当前页
		if (isset ( $this->htmlShow [9] )) {
			$this->htmlShow [9] = str_replace ( "%page", $this->page, $this->htmlShow [9] );
			$linkPage .= str_replace ( "%uri", $this->uri, $this->htmlShow [9] );
		} else {
			$linkPage .= "&nbsp;{$this->page}&nbsp;";
		}
		
		for($i = 1; $i <= $inum; $i ++) {
			$page = $this->page + $i;
			if ($page <= $this->pageNum) {
				if (isset ( $this->htmlShow [5] )) {
					$data = str_replace ( "%page", $page, $this->htmlShow [5] );
					$linkPage .= str_replace ( "%uri", $this->uri, $data );
				} else {
					$linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
				}
			} else
				break;
		}
		return $linkPage;
	}
	private function next() {
		$html = "";
		if ($this->page == $this->pageNum)
			$html .= '';
		else if (isset ( $this->htmlShow [6] )) {
			$this->htmlShow [6] = str_replace ( "%page", $this->page + 1, $this->htmlShow [6] );
			$html .= str_replace ( "%uri", $this->uri, $this->htmlShow [6] );
		} else {
			$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page + 1) . "'>{$this->config["next"]}</a>&nbsp;&nbsp;";
		}
		return $html;
	}
	private function last() {
		$html = "";
		if ($this->page == $this->pageNum)
			$html .= '';
		else if (isset ( $this->htmlShow [7] )) {
			$this->htmlShow [7] = str_replace ( "%uri", $this->uri, $this->htmlShow [7] );
			$html .= str_replace ( "%page", $this->pageNum, $this->htmlShow [7] );
		} else {
			$html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->pageNum) . "'>{$this->config["last"]}</a>&nbsp;&nbsp;";
		}
		
		return $html;
	}
	private function goPage() {
		if (isset ( $this->htmlShow [8] )) {
			$this->htmlShow [8] = str_replace ( "%uri", $this->uri, $this->htmlShow [8] );
			$this->htmlShow [8] = str_replace ( "%page", $this->pageNum, $this->htmlShow [8] );
			$this->htmlShow [8] = str_replace ( "%pageNum", $this->pageNum, $this->htmlShow [8] );
			return $this->htmlShow [8];
		} else {
			return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" value="' . $this->page . '" style="width:25px"><input type="button"  class  = "bg-yellow" style="width:32px;margin-left:3px;" value="GO" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'">&nbsp;&nbsp;';
		}
	}
	
	public function get_go_page() {
	    return 	$this->uri."&page=" . $this->pageNum;
		/* $gopage = '<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>%Num)?%Num:this.value;location=\'%url&page=\'+page+\'\'}" value="%page" class="span2"><a href = "javascript:void(0);" id = "page_skip" font-size: 20px;" onclick="javascript:var page=(this.previousSibling.value>%Num)?%Num:this.previousSibling.value;location=\'%uri&page=\'+page+\'\'"><i class="icon-circle-arrow-right"></i></a>';
		$gopage = str_replace ( "%uri", $this->uri, $gopage );
		$gopage = str_replace ( "%page", $this->page, $gopage );
		$gopage = str_replace ( "%Num", $this->pageNum, $gopage );
		return $gopage; */
	}
	
	function fpage($display = array(0,1,2,3,4,5,6,7,8)) {
		$html [0] = "&nbsp;&nbsp;共有<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;";
		$html [1] = "&nbsp;&nbsp;每页显示<b>" . ($this->end () - $this->start () + 1) . "</b>条，本页<b>{$this->start()}-{$this->end()}</b>条&nbsp;&nbsp;";
		$html [2] = "&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;";
		
		$html [3] = $this->first ();
		
		$html [4] = $this->prev ();
		$html [5] = $this->pageList ();
		$html [6] = $this->next ();
		
		$html [7] = $this->last ();
		$html [8] = $this->goPage ();
		$fpage = '';
		foreach ( $display as $index ) {
			$fpage .= $html [$index];
		}
		return $fpage;
	}
	public function getListRow() {
		return $this->listRows;
	}
	public function getPage() {
		return $this->page;
	}
	
	/*
	 * -----------------------------------------------/ 说明：设置分页需要显示的html 参数： $html:需要数字替换的数组 注意：必须在显示之前被调用 -------------------------------------------------
	 */
	public function setHtml($html) {
		$this->htmlShow = $html;
		// %page:当前页
		// %uri:uri资源
		/*
		 * array( 4=>"<a href='%uri&page=%page'>上一页</a>", //上一页按钮 5=>"<a href='%uri&page=%page'>%page</a>",//当前的页码列表 6=>"<a href='%uri&page=%page'>下一页</a>",//下一页按钮 9=>"<a href='%uri&page=%page'>%page</a>",//当前的页面（在页码列表里面的状态） );
		 */
	}
}
